/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.attendance.clazz.api;

import brave.propagation.TraceContext;
import brave.propagation.TraceIdContext;
import com.arisgo.cloud.attendance.clazz.core.ArrangeContext;
import com.arisgo.cloud.attendance.clazz.service.ClassInfoService;
import com.arisgo.cloud.attendance.clazz.vo.ClassInfoDetailVo;
import com.arisgo.cloud.attendance.clazz.vo.ClassInfoVo;
import com.arisgo.cloud.attendance.clazz.vo.TemporaryArrangeVo;
import com.arisgo.cloud.common.Pagination;
import com.arisgo.cloud.common.Result;
import com.arisgo.cloud.web.param.Paging;
import com.arisgo.cloud.web.param.Search;
import io.lettuce.core.tracing.TraceContextProvider;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * 班 Api
 *
 * @author Coulson
 * @since 2024-12-31
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/class/info")
@Tag(name = "班 API", description = "班CRUD操作")
public class ClassInfoApi {

    private final ClassInfoService classInfoService;

    /**
     * 依id查询班
     */
    @GetMapping(value = "/{id}")
    @Operation(summary = "依id查询")
    public Result<ClassInfoVo> findById(@PathVariable(value = "id") Long id) {
        log.info("测试内容：{}", id);

        return Result.success(classInfoService.findById(id));
    }

    /**
     * 分页查询班
     */
    @PostMapping(value = "/page")
    @Operation(summary = "分页查询")
    public Result<Pagination<ClassInfoVo>> page(@RequestBody Paging paging) {
        return Result.success(classInfoService.page(paging));
    }

    /**
     * 条件查询班
     */
    @PostMapping(value = "/find")
    @Operation(summary = "条件查询")
    public Result<List<ClassInfoVo>> find(@RequestBody Search search) {
        return Result.success(classInfoService.findByParams(search));
    }

    /**
     * 保存班
     */
    @PostMapping
    @Operation(summary = "新增数据")
    public Result<ClassInfoVo> save(@RequestBody ClassInfoVo vo) {
        return Result.success(classInfoService.save(vo));
    }

    /**
     * 更新班
     */
    @PutMapping(value = "/{id}")
    @Operation(summary = "更新数据")
    public Result<Boolean> update(@PathVariable(value = "id") Long id, @RequestBody ClassInfoVo vo) {
        vo.setId(id);
        return Result.success(classInfoService.update(vo));
    }

    /**
     * 逻辑删除班
     */
    @DeleteMapping(value = "/{id}")
    @Operation(summary = "单个逻辑删除")
    public Result<Boolean> delete(@PathVariable(value = "id") Long id) {
        return Result.success(classInfoService.delete(id));
    }

    /**
     * 逻辑删除班
     */
    @DeleteMapping
    @Operation(summary = "批量逻辑删除")
    public Result<Boolean> delete(@RequestBody List<Long> ids) {
        return Result.success(classInfoService.delete(ids));
    }

    /**
     * 物理删除班
     */
    @DeleteMapping(value = "/remove/{id}")
    @Operation(summary = "单个物理删除")
    public Result<Boolean> remove(@PathVariable(value = "id") Long id) {
        return Result.success(classInfoService.removeClassInfo(id));
    }

    /**
     * 物理删除班
     */
    @DeleteMapping(value = "/remove")
    @Operation(summary = "批量物理删除")
    public Result<Boolean> remove(@RequestBody List<Long> ids) {
        return Result.success(classInfoService.remove(ids));
    }

    /**
     * TODO 时间参数，传周为周，传月为月，没有天
     * 获取员工排班信息
     */
    @GetMapping(value = "/staff/{staffId}")
    @Operation(summary = "查询员工班次信息")
    public Result<List<ClassInfoDetailVo>> findClassInfoByStaffId(@PathVariable(value = "staffId") Long staffId,
                                                                  @RequestParam(value = "tenantId") Long tenantId,
                                                                  @RequestParam(value = "startDate", required = false) LocalDate startDate,
                                                                  @RequestParam(value = "endDate", required = false) LocalDate endDate) {
        return Result.success(classInfoService.findByStaffId(tenantId, staffId, startDate, endDate));
    }

    @GetMapping
    @Operation(summary = "查询班次信息")
    public Result<List<ClassInfoDetailVo>> findClassInfo(@RequestParam(value = "tenantId") Long tenantId,
                                                         @RequestParam(value = "startDate", required = false) LocalDate startDate,
                                                         @RequestParam(value = "endDate", required = false) LocalDate endDate) {
        return Result.success(classInfoService.findClassInfo(tenantId, startDate, endDate));
    }

    /**
     * 查询班次信息
     */
    @GetMapping(value = "/detail")
    @Operation(summary = "条件查询")
    public Result<List<ClassInfoDetailVo>> detail(@RequestParam(value = "tenantId") Long tenantId,
                                                  @RequestParam(value = "workday", required = false) LocalDate workday) {
        return Result.success(classInfoService.findClassDetail(tenantId, workday));
    }

    /**
     * 排班
     * @param type 排班类型 standard: 正常排班 / temporary: 临时排班 / auto: 自动排班
     */
    @PostMapping(value = "/arrange/{type}")
    @Operation(summary = "排班")
    public Result<Boolean> arrange(@PathVariable(value = "type") String type,
                                   @RequestBody ArrangeContext context) {
        return Result.success(classInfoService.arrange(type, context));
    }

    /**
     * 临时排班
     */
    @PostMapping(value = "/arrange")
    @Operation(summary = "临时排班")
    public Result<Boolean> arrange(@RequestBody TemporaryArrangeVo vo) {
        return Result.success(classInfoService.arrange(vo));
    }

}
